package com.jams.goodox.activity.mine.buy;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.facebook.common.internal.Objects;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.activity.qa.entity.CourseSeriousId;
import com.jams.goodox.adapter.lfadapter.CommonRecycleViewAdapter;
import com.jams.goodox.adapter.lfadapter.LFRecyclerViewHolder;
import com.jams.goodox.net.services.LoginService;
import com.jams.goodox.utils.StatusBarCompat;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.utils.alipay.Result;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import me.leefeng.lfrecyclerview.LFRecyclerView;
import me.leefeng.lfrecyclerview.OnItemClickListener;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2018/7/8.
 * 添加系列课程
 */

public class NewSerialActivity extends BaseActivity  {

    private static final String TAG = "NewSerialActivity";

    @Bind(R.id.input_box)
    EditText input_box;
    @Bind(R.id.submit)
    TextView submit;

    @Override
    protected int getLayoutId() {
        return R.layout.record_create_activity;
    }

    @Override
    protected void init() {
        StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.main_color));//沉侵式头部
        StyleUtil.titleBackKey(this, "添加课程系列");

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(TextUtils.isEmpty(input_box.getText())){
                    ToastUtil.show("请输入您的课程系列名字");
                    return;
                }
                createCourseSerious();
            }
        });
    }


    private void createCourseSerious(){
        clearMap();

        StringBuilder pData = new StringBuilder();
        pData.append("name:" + input_box.getText())
                .append(",memberId:" + SePref.getId());

        params.put("requestData", "{" + pData.toString() + "}");


        String strEntity = gson.toJson(params);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8")
                ,strEntity);

        showLoading();
        Call<ResponseBody> example = createCallService(LoginService.class).addCourseSerious(body);

        example.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String data = response.body().string();
                    Log.d(TAG, "createCourseSerious = " + data);
                    JSONObject jsonObject = new JSONObject(data);

                    if(200 == jsonObject.getInt("code")){
                        ToastUtil.show("创建课程系列成功");
                        setResult(1);
                        finish();

                    }else {
                        if(jsonObject.has("error")){
                            ToastUtil.show("error:" + jsonObject.getString("error") );
                        }else {
                            ToastUtil.show("网络异常！" );
                        }
                    }

                    misLoading();

                } catch (Exception e) {
                    ToastUtil.show("Exception:" + e.getMessage());
                    misLoading();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                misLoading();
                Log.d(TAG,"onFailure = " + t.getMessage());
            }
        });
    }

}
